package com.yanqu.road.server.gameplayer.module.resource;

import com.yanqu.road.entity.config.beauty.BeautyInfo;
import com.yanqu.road.entity.config.patrons.PatronsInfo;
import com.yanqu.road.entity.config.skin.SkinInfo;
import com.yanqu.road.entity.player.GoodsFirstGetTimeData;
import com.yanqu.road.entity.player.GoodsResourData;
import com.yanqu.road.logic.bussiness.player.UserGoodsResourceBusiness;
import com.yanqu.road.pb.resource.ResourceProto;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.base.GeneralModule;
import com.yanqu.road.server.gameplayer.module.player.BagModule;
import com.yanqu.road.server.logic.resource.GoodsResourceLogic;
import com.yanqu.road.server.manger.config.BeautyMgr;
import com.yanqu.road.server.manger.config.PatronsMgr;
import com.yanqu.road.server.manger.config.SkinMgr;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.utils.string.StringUtils;

import java.util.*;

public class GoodsResourceModule extends GeneralModule {

    private Map<Integer, GoodsResourData> resourDataMap;
    private Map<Integer, GoodsFirstGetTimeData> goodsFirstGetTimeDataMap;

    public GoodsResourceModule(GamePlayer player) {
        super(player);
    }

    @Override
    public boolean loadData() {
        resourDataMap = UserGoodsResourceBusiness.getGoodsResourData(getUserId());
        goodsFirstGetTimeDataMap = UserGoodsResourceBusiness.getGoodsFirstGetData(getUserId());
        return true;
    }

    public void addGoodsFirstGetData(int goodsId,long time) {
        GoodsFirstGetTimeData data = goodsFirstGetTimeDataMap.get(goodsId);
        if (data == null) {
            data = new GoodsFirstGetTimeData();
            data.setUserId(getUserId());
            data.setGoodsId(goodsId);
            data.setFirstGetTime(time);
            data.setInsertOption();
            goodsFirstGetTimeDataMap.put(goodsId, data);
        }
    }

    public void addResource(int goodsId, int linkType) {
        boolean needSync = false;
        GoodsResourData data = resourDataMap.get(goodsId);
        if (data == null) {
            data = new GoodsResourData();
            data.setUserId(getUserId());
            data.setGoodsId(goodsId);
            data.setLinkType(linkType);
            data.setInsertOption();
            resourDataMap.put(goodsId, data);
            needSync = true;
        }
        if (needSync) {
            syncSingleResoure(data);
        }
    }

    private void syncSingleResoure(GoodsResourData data) {
        syncResoure(Collections.singletonList(data));
    }

    @Override
    public void loginSendMsg() {
        checkGoodsFirstGetTime();
        if (resourDataMap != null) {
            syncResoure(new ArrayList<>(resourDataMap.values()));
        }
    }

    private void checkGoodsFirstGetTime() {
        List<Integer> goodsIdList = player.getModule(BagModule.class).getGoodsIdList();
        for (Integer goodsId : goodsIdList) {
            //写死 2021-07-01
            addGoodsFirstGetData(goodsId,1625068800000L);
        }
    }

    public void syncResoure(Collection<GoodsResourData> dataList) {
        ResourceProto.SyncGoodsResourceMsg.Builder syncMsg = ResourceProto.SyncGoodsResourceMsg.newBuilder();
        try {
            Map<Integer, BeautyInfo> beautyInfoMap = BeautyMgr.getBeautyInfoMap();
            for (BeautyInfo info : beautyInfoMap.values()) {
                ResourceProto.GoodsResourceFlagMsg.Builder msg = ResourceProto.GoodsResourceFlagMsg.newBuilder();
                int linkType = BeautyMgr.getLinkType(info.getBeautyId());
                if (linkType == 0) {
                    GoodsResourData data = resourDataMap.get(info.getBeautyId());
                    if (data != null) {
                        linkType = data.getLinkType();
                    }
                }
                msg.setGoodsId(info.getBeautyId());
                msg.setLinkType(linkType);
                syncMsg.addData(msg);
            }

            Map<Integer, SkinInfo> skinInfoMap = SkinMgr.getSkinInfoMap();
            for (SkinInfo info : skinInfoMap.values()) {
                ResourceProto.GoodsResourceFlagMsg.Builder msg = ResourceProto.GoodsResourceFlagMsg.newBuilder();
                int linkType = SkinMgr.getLinkType(info.getSkinId(),getGoodsFirstGetTime(info.getSkinId()));
                if (linkType == 0) {
                    GoodsResourData data = resourDataMap.get(info.getSkinId());
                    if (data != null) {
                        linkType = data.getLinkType();
                    }
                }
                msg.setGoodsId(info.getSkinId());
                msg.setLinkType(linkType);
                syncMsg.addData(msg);
            }

            Map<Integer, PatronsInfo> patronsInfoMap = PatronsMgr.getPatronsInfoMap();
            for (PatronsInfo info : patronsInfoMap.values()) {
                ResourceProto.GoodsResourceFlagMsg.Builder msg = ResourceProto.GoodsResourceFlagMsg.newBuilder();
                int linkType = PatronsMgr.getLinkType(info.getId());
                if (linkType == 0) {
                    GoodsResourData data = resourDataMap.get(info.getId());
                    if (data != null) {
                        linkType = data.getLinkType();
                    }
                }
                msg.setGoodsId(info.getId());
                msg.setLinkType(linkType);
                syncMsg.addData(msg);
            }
      /*



        for (GoodsResourData data : dataList) {
            ResourceProto.GoodsResourceFlagMsg.Builder msg = ResourceProto.GoodsResourceFlagMsg.newBuilder();
            msg.setGoodsId(data.getGoodsId());
            msg.setLinkType(data.getLinkType());
            syncMsg.addData(msg);
        }*/
            player.sendPacket(ClientProtocol.U_GOODS_RESOURCE_SYNC, syncMsg);
        } catch (Exception e) {
            log.error("{} sync goods source error",getUserId(),e);
        }

    }

    @Override
    public boolean afterLoadData() {
        return true;
    }

    @Override
    public boolean saveData() {
        if (resourDataMap != null && !resourDataMap.isEmpty()) {
            ArrayList<GoodsResourData> list = new ArrayList<>(resourDataMap.values());
            for (GoodsResourData data : list) {
                if (data.isInsertOption()) {
                    UserGoodsResourceBusiness.addGoodsResourData(data);
                } else if (data.isUpdateOption()) {
                    UserGoodsResourceBusiness.updateGoodsResourData(data);
                }
            }
        }
        if (goodsFirstGetTimeDataMap != null) {
            for (GoodsFirstGetTimeData data : new ArrayList<>(goodsFirstGetTimeDataMap.values())) {
                if (data.isInsertOption()) {
                    UserGoodsResourceBusiness.addGoodsFirstGetData(data);
                }
            }
        }
        return true;
    }

    public long getGoodsFirstGetTime(int skinId) {
        GoodsFirstGetTimeData data = goodsFirstGetTimeDataMap.get(skinId);
        if (data != null) {
            return data.getFirstGetTime();
        }
        return System.currentTimeMillis();
    }

    /**
    * 描述：获取门客联动类型
    * 作者：zrq
    * 时间：2022/1/4
    * 参数：
    门客编号
    * 返回值：联动类型
    **/
    public int getPatronsLinkType(int patronsId){
        int linkType = PatronsMgr.getLinkType(patronsId);
        if (linkType == 0) {
            GoodsResourData data = resourDataMap.get(patronsId);
            if (data != null) {
                linkType = data.getLinkType();
            }
        }
        return linkType;
    }
}
